<template>
  <div class="container">
    <div class="product-images">
      <div
          class="main-image"
          :style="{ backgroundImage: `url(${currentImage})` }"
      ></div>
      <div class="thumbnails">
        <div
            v-for="(image, index) in productImages"
            :key="index"
            class="thumbnail"
            :class="{ active: image === currentImage }"
            @click="selectImage(image)"
            :style="{ backgroundImage: `url(${image})` }"
        ></div>
      </div>
    </div>
    <div class="product-info">
      <h1 class="product-title">{{ product.title }}</h1>
      <div class="product-price">¥{{ product.price/100 }}</div>
      <div class="product-specs">
        <h3>商品规格</h3>
        <div class="spec-item" v-for="(spec, key) in product.specs" :key="key">
          {{ spec.name }}:
          {{ isArray(spec.values) ? spec.values.join('、') : spec.values }}
        </div>
      </div>
      <div class="product-actions">
        <button class="btn btn-primary" @click="addToCart">加入购物车</button>
        <button class="btn btn-buy" @click="buyNow">立即购买</button>
        <button class="btn btn-favorite" @click="addToFavorite">
          <i class="icon-heart" :class="{'filled': isFavorite}"></i>
          {{ isFavorite ? '取消收藏' : '收藏商品' }}
        </button>
      </div>
    </div>
  </div>
</template>

<script setup>
import {ref} from 'vue';
const isFavorite = ref(false)

const isArray = (value) => {
  return Array.isArray(value);
}
const props = defineProps({
  product: {
    type: Object,
    required: true
  }
})
const emit = defineEmits(['addToCart', 'buyNow', 'addToFavorite'])
const currentImage = ref(props.product.images[0])
const productImages = ref(props.product.images)
const selectImage = (image) => {
  currentImage.value = image
}

const addToCart = () => {
  emit('addToCart')
}

const buyNow = () => {
  emit('buyNow')
}

const addToFavorite = () => {
  emit('addToFavorite')
}

defineExpose({
  isFavorite
})
</script>

<style scoped>
.container {
  max-width: 1200px;
  margin: 20px auto;
  padding: 20px;
  display: flex;
  gap: 30px;
}

.product-images {
  flex: 1;
}

.main-image {
  width: 100%;
  height: 400px;
  background-color: #fff;
  background-size: contain;
  background-position: center;
  background-repeat: no-repeat;
  margin-bottom: 10px;
  border-radius: 8px;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
}

.thumbnails {
  display: flex;
  gap: 10px;
}

.thumbnail {
  width: 80px;
  height: 80px;
  background-color: #fff;
  background-size: contain;
  background-position: center;
  background-repeat: no-repeat;
  cursor: pointer;
  border: 2px solid transparent;
  border-radius: 4px;
  transition: all 0.3s ease;
}

.thumbnail.active {
  border-color: #007BFF;
}

.product-info {
  flex: 1;
}

.product-title {
  font-size: 24px;
  margin-bottom: 10px;
  color: #212529;
}

.product-price {
  font-size: 28px;
  color: #e60012;
  margin-bottom: 20px;
  font-weight: bold;
}


.product-specs {
  margin-bottom: 20px;
  padding: 15px;
  background-color: #f8f9fa;
  border-radius: 5px;
}

.spec-item {
  margin-bottom: 5px;
  padding: 5px 0;
}

.purchase-actions {
  display: flex;
  gap: 10px;
  margin-top: 20px;
}

.btn {
  padding: 12px 25px;
  border: none;
  border-radius: 5px;
  cursor: pointer;
  font-size: 16px;
  transition: all 0.3s ease;
}

.btn-primary {
  background-color: #007BFF;
  color: white;
}

.btn-primary:hover {
  background-color: #0069d9;
}

.btn-favorite {
  background-color: transparent;
  color: #666;
  border: 1px solid #ddd;
}

.btn-favorite:hover {
  border-color: #f44336;
  color: #f44336;
}

.product-actions {
  display: flex;
  gap: 15px;
  margin-top: 30px;
}

.btn {
  padding: 12px 24px;
  border: none;
  border-radius: 4px;
  font-size: 16px;
  font-weight: 500;
  cursor: pointer;
  transition: all 0.3s ease;
  display: flex;
  align-items: center;
  justify-content: center;
}


.btn-buy {
  background-color: #FFD700;
  color: #333;
  border: 1px solid #D4AF37;
  box-shadow: 0 2px 5px rgba(212, 175, 55, 0.3);
}

.btn-buy:hover {
  background-color: #D4AF37;
  color: #fff;
  box-shadow: 0 4px 8px rgba(212, 175, 55, 0.4);
}

.btn-favorite {
  background-color: transparent;
  color: #666;
  border: 1px solid #ddd;
}

.btn-favorite:hover {
  border-color: #f44336;
  color: #f44336;
}

.icon-heart {
  margin-right: 8px;
  font-size: 18px;
}

.icon-heart.filled {
  color: #f44336;
}
</style>